Make git work in the dev container when opened from a worktree#2803
Merged
Conversation
When the dev container is opened from a git worktree, the worktree's .git is a file whose `gitdir:` line points at a host path under the main repo's .git/worktrees/<branch>. That host path is not visible inside the container by default, so git inside the container fails with "fatal: not a git repository: (null)" -- VS Code's safe.directory probe, credential.helper setup, and any developer git command all hit this. Resolve the common git dir on the host in initializeCommand.sh via `git rev-parse --git-common-dir`, write the absolute path into .devcontainer/.env, and have docker-compose.yml bind-mount it at the same path inside the container. The worktree's `.git` file's gitdir reference then resolves cleanly. For a main checkout this is a no-op-shaped redundant mount of <repo>/.git at the same host path inside the container; nothing breaks. .devcontainer/.env is generated per-launch and is gitignored.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When the dev container is opened from a git worktree (e.g.
<repo>/.worktrees/<branch>/), the worktree's.gitis a file whosegitdir:line points at a host path under the main repo's.git/worktrees/<branch>. That host path is not visible inside the container by default, so git inside the container fails withfatal: not a git repository: (null)— VS Code'ssafe.directoryprobe,credential.helpersetup, and any developer git command all hit this. The dev container build itself completes, but the in-container git workflow is broken.initializeCommand.shalready runs on the host, where git is available. Resolve the common git dir viagit rev-parse --git-common-dir, write the absolute path into.devcontainer/.env, and havedocker-compose.ymlbind-mount that host path at the same path inside the container. The worktree's.gitfile'sgitdir:reference then resolves cleanly.For a main checkout the host path is
<repo>/.git, and mounting it at<repo>/.gitinside the container is a redundant no-op-shaped mount that doesn't conflict with the existing workspace mount.Changes
.devcontainer/initializeCommand.sh— after the TZ extraction, computeGIT_COMMON_DIRand write.devcontainer/.env..devcontainer/docker-compose.yml— bind-mount${GIT_COMMON_DIR}:${GIT_COMMON_DIR}:cachedon theappservice. docker-compose reads.envfrom the directory containingdocker-compose.ymlautomatically..gitignore— ignore.devcontainer/.env(generated per launch).Test plan
<repo>/.worktrees/<branch>/) and confirm:safe.directoryandcredential.helpersetup.git statussucceeds andgit rev-parse --show-toplevelprints/workspaces/oracle-enhanced.<repo>/) and confirm the same — the extra bind mount should be harmless.